package com.ruyuan.eshop.construction.api.impl;

import com.alibaba.fastjson.JSON;
import com.ruyuan.eshop.common.core.JsonResult;
import com.ruyuan.eshop.common.core.PageResult;
import com.ruyuan.eshop.construction.api.BuyerApi;
import com.ruyuan.eshop.construction.domain.dto.BuyerInfoDTO;
import com.ruyuan.eshop.construction.domain.dto.BuyerListDTO;
import com.ruyuan.eshop.construction.domain.dto.BuyerResultDTO;
import com.ruyuan.eshop.construction.domain.request.QueryBuyerListRequest;
import com.ruyuan.eshop.construction.domain.request.QueryBuyerPageRequest;
import com.ruyuan.eshop.construction.domain.request.BuyerRequest;
import com.ruyuan.eshop.common.exception.ProductBizException;
import com.ruyuan.eshop.construction.service.BuyerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 买手服务
 *
 * @author zhonghuashishan
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = BuyerApi.class, retries = 0)
public class BuyerApiImpl implements BuyerApi {

    @Autowired
    private BuyerService buyerService;


    @Override
    public JsonResult<BuyerResultDTO> buyer(BuyerRequest request) {
        try {
            BuyerResultDTO buyerResultDTO = buyerService.saveBuyer(request);
            return JsonResult.buildSuccess(buyerResultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<BuyerListDTO> getBuyerInfo(QueryBuyerListRequest request) {
        try {
            BuyerListDTO buyerListDTO = buyerService.getBuyerInfo(request);
            return JsonResult.buildSuccess(buyerListDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<PageResult<BuyerInfoDTO>> getBuyerInfoPage(QueryBuyerPageRequest request) {
        try {
            PageResult<BuyerInfoDTO> buyerInfoPageResult = buyerService.getBuyerInfoPage(request);
            return JsonResult.buildSuccess(buyerInfoPageResult);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
